package com.ruoyi.fatigue.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.fatigue.domain.entity.DetectionLog;
import com.ruoyi.fatigue.domain.vo.DetectionLogPageQueryVO;
import com.ruoyi.fatigue.domain.vo.DetectionLogPageVO;
import com.ruoyi.fatigue.domain.vo.PieStaVO;
import com.ruoyi.fatigue.service.DetectionLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检测记录接口
 *
 * @author wasd11203
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/fatigue/detection_log")
public class DetectionLogController extends BaseController {

    private final DetectionLogService detectionLogService;

    /**
     * 当前用户的饼图统计
     */
    @GetMapping("/cur/pie_sta")
    public R<PieStaVO> curPieSta() {
        PieStaVO result = new PieStaVO();
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("用户未登录");
        }
        Long userId = loginUser.getUserId();
        if (userId == null) {
            throw new ServiceException("用户未登录");
        }
        QueryWrapper<DetectionLog> detectionLogQueryWrapper = new QueryWrapper<>();
        detectionLogQueryWrapper.eq("user_id", userId);
        List<DetectionLog> list = detectionLogService.list(detectionLogQueryWrapper);
        if (CollUtil.isEmpty(list)) {
            return R.ok(result);
        }

        long eyeCount = list.stream().filter(item -> item.getEyeStatus() == 1).count();
        long nodCount = list.stream().filter(item -> item.getNodStatus() == 1).count();
        long yawnCount = list.stream().filter(item -> item.getYawnStatus() == 1).count();

        result.setEyeCount(eyeCount);
        result.setNodCount(nodCount);
        result.setYawnCount(yawnCount);
        result.setAllCount(Long.valueOf(list.size()));
        return R.ok(result);
    }


    /**
     * 分页获取检测记录
     */
    @GetMapping("/list")
    public TableDataInfo<DetectionLogPageVO> list(@Validated DetectionLogPageQueryVO bo, PageQuery pageQuery) {
        Page<DetectionLogPageVO> result = new Page<>();

        QueryWrapper<DetectionLog> queryWrapper = new QueryWrapper<>();
        Page<DetectionLog> page = detectionLogService.page(pageQuery.build(), queryWrapper);
        List<DetectionLog> records = page.getRecords();
        if (CollUtil.isNotEmpty(records)){
            List<DetectionLogPageVO> list = Convert.toList(DetectionLogPageVO.class, records);
            result.setRecords(list);
        }
        result.setTotal(page.getTotal());
        result.setPages(page.getPages());
        result.setCurrent(page.getCurrent());
        result.setSize(page.getSize());
        return TableDataInfo.build(result);
    }
}
